<template>
	<view class="content">
		<!-- 图片解锁 -->
		<view v-if="details.type == 1" class="picture">
			<swiper class="swiper" circular indicator-dots indicator-color="#C1C1C1" indicator-active-color="#545454">
				<!-- #ifndef H5 -->
				<swiper-item v-for="(v,i) in details.content" :key="i" @longtap="longpress(v)">
					<image :src="v" mode="aspectFit"></image>
				</swiper-item>
				<!-- #endif -->
				<!-- #ifdef H5 -->
				<swiper-item v-for="(v,i) in details.content" :key="i">
					<image :src="v" mode="aspectFit"></image>
				</swiper-item>
				<!-- #endif -->
			</swiper>
			<!-- 			<image class="download" src="https://miaoyao-oss.oss-cn-hangzhou.aliyuncs.com/profitTool/download.png"
				mode="scaleToFill" @click="download"></image> -->
		</view>
		<!-- 视频解锁 -->
		<view v-else-if="details.type == 2" class="videoUrl">
			<video :src="details.content[0]" :show-menu-by-longpress="true"></video>
		</view>
		<!-- 地图解锁 -->
		<view v-else-if="details.type == 3" class="plat">
			<map id="myMap" class="map" :latitude="details.latitude" :longitude="details.longitude" :scale="14"
				:enable-scroll="false" :enable-zoom="false" :show-location="true"></map>
			<view class="btn" @click="goTo">去导航</view>
		</view>
		<!-- 文本文案 -->
		<view v-if="details.type == 1 || details.type == 2" class="msg">
			<view class="user" @click="navigateTo(`/pages/index/space?id=${details.user.id}`)">
				<image :src="details.user.avatar" mode="scaleToFill"></image>
				<text>{{details.user.nickname}}</text>
			</view>
			<view class="desc">
				{{details.desc}}
			</view>
			<view class="category">
				🏷️#{{details.category_id | categoryHandle}}
			</view>
		</view>
		<!-- 点赞收藏分享 -->
		<view class="operateBor">
			<view class="item">
				<image v-if="details.is_zan" src="https://miaoyao-oss.oss-cn-hangzhou.aliyuncs.com/profitTool/cdz1.png"
					mode="scaleToFill" @click="doFavorite(2)"></image>
				<image v-else src="https://miaoyao-oss.oss-cn-hangzhou.aliyuncs.com/profitTool/cdz2.png"
					mode="scaleToFill" @click="doFavorite(2)"></image>
				<text>{{details.zan_num}}</text>
			</view>
			<view class="item">
				<image v-if="details.is_collect"
					src="https://miaoyao-oss.oss-cn-hangzhou.aliyuncs.com/profitTool/cxx1.png" mode="scaleToFill"
					@click="doFavorite(1)"></image>
				<image v-else src="https://miaoyao-oss.oss-cn-hangzhou.aliyuncs.com/profitTool/cxx2.png"
					mode="scaleToFill" @click="doFavorite(1)"></image>
				<text>{{details.collect_num}}</text>
			</view>
			<view class="item">
				<image src="https://miaoyao-oss.oss-cn-hangzhou.aliyuncs.com/profitTool/cll.png" mode="scaleToFill">
				</image>
				<text>{{details.browse_num}}</text>
			</view>
			<view class="item" @click="showflower = true">
				<image v-if="true" src="https://miaoyao-oss.oss-cn-hangzhou.aliyuncs.com/profitTool/gift/flowerT1.png"
					mode="scaleToFill">
				</image>
				<image v-else src="https://miaoyao-oss.oss-cn-hangzhou.aliyuncs.com/profitTool/gift/flowerF1.png"
					mode="scaleToFill">
				</image>
				<text>{{details.flower_num}}</text>
			</view>
			<view class="item">
				<!-- #ifdef H5 -->
				<image src="https://miaoyao-oss.oss-cn-hangzhou.aliyuncs.com/profitTool/cfx.png" mode="scaleToFill"
					@click="share">
				</image>
				<!-- #endif -->
				<!-- #ifndef H5 -->
				<button class="btn" open-type="share">
					<image src="https://miaoyao-oss.oss-cn-hangzhou.aliyuncs.com/profitTool/cfx.png" mode="scaleToFill">
					</image>
				</button>
				<!-- #endif -->
				<!-- <text>268</text> -->
			</view>
		</view>
		<!-- 下载弹窗 -->
		<u-popup :show="show" :safeAreaInsetBottom="false" @close="closePop">
			<view class="popupBox">
				<view class="save" @click="download">保存</view>
				<view class="save" @click="report">举报</view>
			</view>
		</u-popup>
		<!-- 送礼物弹窗 -->
		<u-popup :show="showflower" mode="center" round="10" :safeAreaInsetBottom="false" @close="closeshowflower">
			<view class="showflower">
				<view class="tis">请设置要赠送的数量</view>
				<u-number-box integer v-model="flower" inputWidth="80"></u-number-box>
				<view class="btns">
					<view class="btn" @click="closeshowflower">取消</view>
					<view class="btn active" @click="sendFlower()">确定</view>
				</view>
			</view>
		</u-popup>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: "",
				details: {},
				imgSrc: "",
				show: false,
				showflower: false,
				flower: 1
			}
		},
		filters: {
			categoryHandle(data) {
				let list = uni.getStorageSync('category')
				if (!data) return
				let res = list.filter(v => {
					return v.id == data
				})
				return res[0].title
			}
		},
		onLoad(options) {
			// console.log(options);
			if (options.referee_id) {
				uni.setStorageSync("referee_id", options.referee_id)
			}
			this.id = options.id
			this.getWorkInfo()
		},
		methods: {
			// 赠送鲜花
			async sendFlower() {
				let res = await this.$http("api/work/sendFlower", {
					work_id: this.id, //true	int		作品id
					flower: this.flower, //true	string		赠送鲜花数量
				})
				if (res.code == 200) {
					this.getWorkInfo()
					uni.showToast({
						title: '赠送成功',
						duration: 2000
					})
					this.showflower = false
					this.flower = 1
				}
			},
			closeshowflower() {
				this.showflower = false
				this.flower = 1
			},
			share() {
				if (getApp().globalData.platform == `web`) {
					// console.log(window.location);
					let userInfo = uni.getStorageSync("user");
					uni.setClipboardData({
						data: `${window.location.href}&referee_id=${userInfo.id}`,
						showToast: false,
						success: () => {
							uni.showToast({
								title: '已复制邀请链接，可发送至邀请人加入',
								duration: 2000
							})
						},
						fail: (err) => {
							console.log(err);
						}
					});
				}
			},
			//收藏/取消收藏/点赞/取消点赞
			async doFavorite(work_type) {
				if (work_type == 1) {
					this.details.is_collect = this.details.is_collect == 1 ? 0 : 1
				} else if (work_type == 2) {
					this.details.is_zan = this.details.is_zan == 1 ? 0 : 1
				}
				let res = await this.$http("api/common/doFavorite", {
					type: 1, //true	int		1作品2用户
					work_type, //false	int		默认可以传0,type等于1需要传,作品操作类型 1收藏 2点赞
					tbl_id: this.id, //true	int		作品id或用户id 取消收藏多个id用,拼接
				})
				this.getWorkInfo()
			},
			closePop() { // 关闭弹框
				this.show = false;
			},
			longpress(v) { // 长按解锁
				// console.log(v);
				this.imgSrc = v;
				this.show = true;
			},
			report() { // 举报
				this.$refs.uToast.show({
					type: "default",
					title: "默认主题",
					message: "已受理",
				});
			},
			//下载图片
			download() {
				// this.details.content.map(v => {
				uni.downloadFile({ //下载
					url: this.imgSrc, //图片下载地址
					success: res => {
						if (res.statusCode === 200) {
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: (e) => {
									// console.log(e);
									uni.showToast({
										title: '下载成功!',
										icon: 'none',
										duration: 2000
									});
								},
								fail: (e) => {
									console.log(e);
									uni.showToast({
										title: '下载失败!',
										icon: 'none',
										duration: 2000
									});
								}
							});
						}
					},
					fail(err) {
						console.log(err);
					}
				});
				// })
			},
			// 前往导航
			goTo() {
				uni.openLocation({
					latitude: Number(this.details.latitude),
					longitude: Number(this.details.longitude),
					name: "位置分享",
					address: this.details.title,
					complete: (res) => {
						console.log('success', res);
					}
				});
			},
			//作品详情
			async getWorkInfo() {
				let res = await this.$http("api/work/getWorkInfo", {
					id: this.id
				}, "GET")
				// console.log(res);
				res.result.content = res.result.content.split(",")
				this.details = res.result
				uni.setNavigationBarTitle({
					title: res.result.title
				});

			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		position: relative;
		padding-bottom: 50rpx;

		.picture {
			width: 100vw;
			// height: 100vh;
			background-color: #fff;
			padding-bottom: 40rpx;
			box-sizing: border-box;
			position: relative;

			.swiper {
				width: 100%;
				// height: 80vh;
				height: 1084rpx;

				image {
					display: block;
					width: 100%;
					height: 100%;
				}
			}

			.download {
				position: absolute;
				top: 50rpx;
				right: 50rpx;
				width: 60rpx;
				height: 60rpx;
				background-color: #000;
				border-radius: 50%;
			}
		}

		.videoUrl {
			video {
				display: block;
				width: 100vw;
				// height: 100vh;
				// height: 60vh;
				height: 1084rpx;
			}
		}

		.plat {
			.map {
				width: 100vw;
				height: 100vh;
			}

			.btn {
				position: fixed;
				left: 50%;
				bottom: 100rpx;
				transform: translateX(-50%);
				z-index: 99;
				width: 686rpx;
				height: 96rpx;
				background: #333333;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				line-height: 96rpx;
				text-align: center;
				font-weight: bold;
				font-size: 32rpx;
				color: #FFFFFF;
			}
		}

		.msg {
			padding: 0 32rpx;
			background-color: #fff;

			.user {
				display: flex;
				align-items: center;
				// justify-content: flex-end;
				padding: 24rpx 0;

				image {
					width: 60rpx;
					height: 60rpx;
					border-radius: 50%;
				}

				text {
					font-weight: 500;
					font-size: 26rpx;
					color: #000000;
					padding-left: 12rpx;
				}
			}

			.desc {
				font-weight: bold;
				font-size: 28rpx;
				color: #333333;
				line-height: 42rpx;
				letter-spacing: 2rpx;
			}

			.category {
				font-weight: bold;
				font-size: 28rpx;
				color: #FF504A;
				letter-spacing: 2rpx;
				padding: 24rpx 0;
			}
		}

		.operateBor {
			display: flex;
			flex-direction: column;
			align-items: center;
			// justify-content: space-around;
			position: absolute;
			top: 250rpx;
			right: 32rpx;
			width: 116rpx;
			// height: 480rpx;
			padding: 16rpx 0;
			background: rgba(0, 0, 0, 0.2);
			border-radius: 18rpx 18rpx 18rpx 18rpx;
			border: 2rpx solid rgba(255, 255, 255, 0.7);
			z-index: 100;

			.item {
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				padding: 16rpx 0;

				image {
					width: 56rpx;
					height: 56rpx;
				}

				text {
					padding-top: 12rpx;
					font-weight: 500;
					font-size: 28rpx;
					color: #FFFFFF;
				}

				::v-deep button {
					background: none;
					padding: 0;
					text-align: left;
					margin: 0;
				}

				::v-deep button::after {
					content: none;
				}
			}
		}
	}

	.popupBox {
		display: flex;
		justify-content: center;
		background: #eee;
		padding: 20rpx 0 40rpx;

		.save:before {
			content: "";
			display: block;
			width: 40rpx;
			height: 40rpx;
			margin: 0 auto;
			background: url(https://oss.miaoyao.xin/shop/20250113/1736747444utXXqPTg.png);
			background-size: 100% 100%;
			margin: 10rpx auto 5rpx;
		}

		.save:nth-of-type(2):before {
			background: url(https://oss.miaoyao.xin/shop/20250113/1736747543Z1cLAkMD.png);
			background-size: 100% 100%;
		}

		.save {
			width: 300rpx;
			height: 100rpx;
			background: #fff;
			font-size: 30rpx;
			margin: 0 auto;
			text-align: center;
			overflow: hidden;
			border-radius: 10rpx;
		}
	}

	.showflower {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 420rpx;
		// height: 340rpx;
		background: #FFFFFF;
		border-radius: 24rpx 24rpx 24rpx 24rpx;
		text-align: center;

		.tis {
			font-weight: bold;
			font-size: 28rpx;
			color: #333333;
			padding: 50rpx 0;
		}

		.btns {
			display: flex;
			justify-content: center;
			padding: 50rpx 0;

			.btn {
				width: 168rpx;
				height: 52rpx;
				background: #F8F8F8;
				border-radius: 8rpx;
				text-align: center;
				line-height: 52rpx;
				margin: 0 12rpx;
				font-size: 24rpx;
			}

			.active {
				background: #FF504A;
				color: #fff;
			}
		}
	}
</style>